<?php

namespace App\Http\Services;

class BsblyService
{
    const DOMAIN = "https://weapp.bsbly.com/api/";
    const USER_GET_URL = "auth/member";
    const USER_GET_TOKEN = "auth/access_token";
    private $access_token = "";
    private $appid = "wx0842f4f0457b62b8";

    public function getUserInfo($unionid)
    {
        $this->getAccessToken($unionid);
        $url = self::DOMAIN . self::USER_GET_URL . '?unionid=' . $unionid . '&access_token=' . $this->access_token;
        $res = DoGet($url);
        return json_decode($res, true);
    }

    /**
     * 获取会员信息
     */
    public function getAccessToken($unionid)
    {
        if (empty($this->appid))
            throw new \Exception("AppId 异常");

        if (empty($unionid))
            throw new \Exception("Unionid 必填");

        $url = self::DOMAIN . self::USER_GET_TOKEN . '?unionid=' . $unionid . '&appid=' . $this->appid;
        $res = json_encode(DoGet($url), true);
        $this->access_token = empty($res['token']) ? "" : $res['token'];
    }

}